/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import dbHandling.DatabaseConnect;
import java.sql.SQLException;
import java.util.Properties;
import javafx.geometry.HPos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javax.persistence.PersistenceException;

/**
 *
 * @author Joseph
 */
public class DatabaseView extends GridPane {

    private TextField hostIPField;
    private Label hostIPLabel;
    private TextField portField;
    private Label portLabel;
    private TextField databaseField;
    private Label databaseLabel;
    private TextField userField;
    private Label userLabel;
    private TextField passField;
    private Label passLabel;
    private Properties properties;
    private Label errorLabel;

    public DatabaseView(final BorderPane pane, final DatabaseConnect dbc, Exception exc) {
        properties = dbc.getProperties();
        this.passLabel = new Label("Password:");
        this.passField = new TextField(properties.getProperty("password"));
        this.userLabel = new Label("Username:");
        this.userField = new TextField(properties.getProperty("username"));
        this.databaseLabel = new Label("Database:");
        this.databaseField = new TextField(properties.getProperty("database"));
        this.databaseField.setDisable(true);
        this.portLabel = new Label("Port:");
        this.portField = new IntegerTextField();
        portField.setText(properties.getProperty("port"));
        this.hostIPLabel = new Label("Host/IP:");
        this.hostIPField = new TextField(properties.getProperty("host/ip"));
        if (exc instanceof SQLException) {
            errorLabel = new Label(getErrorMessage((SQLException) exc));
        } else if (exc instanceof PersistenceException) {
            errorLabel = new Label(getErrorMessage((PersistenceException) exc));
        } else {
            errorLabel = new Label("Mangler MySQL driver - mysql-connector-java-5.1.30.jar");
        }
        Button submitButton = new Button("Submit");
        submitButton.setMinWidth(100);
        this.setVgap(10);
        this.setPrefSize(300, 300);
        this.getColumnConstraints().add(new ColumnConstraints(30));
        this.getColumnConstraints().add(new ColumnConstraints(100));
        this.add(hostIPLabel, 1, 1);
        this.add(hostIPField, 2, 1);
        this.add(new Label(" "), 3, 1);
        this.add(portLabel, 1, 2);
        this.add(portField, 2, 2);
        this.add(databaseLabel, 1, 3);
        this.add(databaseField, 2, 3);
        this.add(userLabel, 1, 4);
        this.add(userField, 2, 4);
        this.add(passLabel, 1, 5);
        this.add(passField, 2, 5);
        this.add(submitButton, 1, 6, 2, 1);
        this.add(errorLabel, 1, 7, 3, 1);

        submitButton.setOnAction(event -> {
            properties.setProperty("username", userField.getText());
            properties.setProperty("password", passField.getText());
            //properties.setProperty("database", databaseField.getText());
            properties.setProperty("host/ip", hostIPField.getText());

            properties.setProperty("port", portField.getText());

            dbc.saveProperties(properties);
            try {
                dbc.connectToDB();
                pane.getChildren().remove(this);
            } catch (Exception ex) {
                errorLabel.setText(getErrorMessage(ex));
            }
        });

        GridPane.setHalignment(submitButton, HPos.CENTER);

    }

    private String getErrorMessage(Exception ex) {
        String message = "";
        if (ex.getMessage().contains("Error Code: 0")) {
            message = "Server svarer ikke eller er ikke startet op. Måske forkert Host/IP eller Port";
        } else if (ex.getMessage().contains("Error Code: 1049")) {
            message = "Databasen er ikke oprettet i serveren";
        } else if (ex.getMessage().contains("Error Code: 1045")) {
            message = "Adgang nægtet: forkert Username/Password";
        }
        return message;
    }

}
